package edu.westga.recommender.GUI;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import java.io.File;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import edu.westga.recommender.model.*;
import edu.westga.recommender.controller.*;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RecommenderGUI extends javax.swing.JFrame {
	private JButton jButtonRecommend;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JButton jButtonOpenFile;
	private JFileChooser jFileChooser1;
	private JTextArea jTextAreaRecommendations;
	private JLabel jLabel1;
	private JTextField jTextFieldNSize;
	private JTextField jTextFieldUserID;
	private File recsFile = new File("recommendations.txt");
	private JRadioButton jRadioButtonItemRecs;
	private JRadioButton jRadioButtonUserRec;
	private ItemRecommenderController IRC;
	private UserRecommenderController URC;
	private ItemItemSimilarity IIS = new ItemItemSimilarity();
	private UserNeighborSimilarity UNS = new UserNeighborSimilarity();
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				RecommenderGUI inst = new RecommenderGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public RecommenderGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Recommender");
			{
				jButtonRecommend = new JButton();
				jButtonRecommend.setText("Recommend");
				jButtonRecommend.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						jButtonRecommendMouseClicked(evt);
					}
				});
			}
			{
				jTextFieldNSize = new JTextField();
				jTextFieldNSize.setEnabled(false);
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setText("User ID:");
			}
			{
				jTextFieldUserID = new JTextField();
			}
			{
				jLabel2 = new JLabel();
				jLabel2.setText("Neiborhood:");
			}
			{
				jTextAreaRecommendations = new JTextArea();
			}
			{
				jRadioButtonUserRec = new JRadioButton();
				jRadioButtonUserRec.setText("User Similarity Based Recommendation");
				jRadioButtonUserRec.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						jRadioButtonUserRecMouseClicked(evt);
					}
				});
			}
			{
				jRadioButtonItemRecs = new JRadioButton();
				jRadioButtonItemRecs.setText("Item Similarity Based Recommendation");
				jRadioButtonItemRecs.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						jRadioButtonItemRecsMouseClicked(evt);
					}
				});
				jRadioButtonItemRecs.setSelected(true);
			}
			{
				jFileChooser1 = new JFileChooser();
			}
			{
				jButtonOpenFile = new JButton();
				jButtonOpenFile.setText("Browse Ratings");
				jButtonOpenFile.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						jButtonOpenFileMouseClicked(evt);
					}
				});
			}
			{
				jLabel3 = new JLabel();
				jLabel3.setText("Recommendations:");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jFileChooser1, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
				.addGap(0, 8, GroupLayout.PREFERRED_SIZE)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(jTextFieldNSize, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jTextFieldUserID, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jButtonOpenFile, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jRadioButtonItemRecs, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jRadioButtonUserRec, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGap(0, 31, GroupLayout.PREFERRED_SIZE)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(jButtonRecommend, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jTextAreaRecommendations, 0, 106, Short.MAX_VALUE)
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addComponent(jFileChooser1, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
				.addGap(12)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(jButtonOpenFile, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
				            .addComponent(jTextFieldUserID, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
				                .addGap(18))
				            .addComponent(jLabel3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE))
				        .addGap(27)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(jButtonRecommend, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(jTextFieldNSize, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))
				            .addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)))
				    .addComponent(jRadioButtonUserRec, GroupLayout.Alignment.LEADING, 0, 301, Short.MAX_VALUE)
				    .addComponent(jRadioButtonItemRecs, GroupLayout.Alignment.LEADING, 0, 301, Short.MAX_VALUE)
				    .addComponent(jTextAreaRecommendations, GroupLayout.Alignment.LEADING, 0, 301, Short.MAX_VALUE))
				.addContainerGap());
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButtonRecommendMouseClicked(MouseEvent evt) {
		System.out.println("jButtonRecommend.mouseClicked");
		try{
			if (jRadioButtonItemRecs.isSelected()){
				IRC = new ItemRecommenderController(Long.parseLong(jTextFieldUserID.getText()), recsFile, IIS);
				jTextAreaRecommendations.setText(IRC.RecommendNow());
			}else if (jRadioButtonUserRec.isSelected()){
				URC = new UserRecommenderController(Long.parseLong(jTextFieldUserID.getText()),Integer.parseInt(jTextFieldNSize.getText()), recsFile, UNS);
				jTextAreaRecommendations.setText(URC.RecommendNow());
			}else{
				jTextAreaRecommendations.setText("Please select the type of Recommendation!!!");
			}
		}catch (Exception e){
			jTextAreaRecommendations.setText("Try again...Wrong input!!!");
		}
	}
	
	private void jButtonOpenFileMouseClicked(MouseEvent evt) {
		System.out.println("jButtonOpenFile.mouseClicked");
		int returnVal = jFileChooser1.showOpenDialog(getParent());
		if (returnVal == JFileChooser.APPROVE_OPTION){
			recsFile = jFileChooser1.getSelectedFile();
		}
		
	}
	
	private void jRadioButtonUserRecMouseClicked(MouseEvent evt) {
		System.out.println("jRadioButtonUserRec.mouseClicked");
		jRadioButtonItemRecs.setSelected(false);
		jTextFieldNSize.setEnabled(true);
	}
	
	private void jRadioButtonItemRecsMouseClicked(MouseEvent evt) {
		System.out.println("jRadioButtonItemRecs.mouseClicked");
		jRadioButtonUserRec.setSelected(false);
		jTextFieldNSize.setEnabled(false);
	}

}
